package com.linkstec.mot.dao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.linkstec.mot.util.ConfigUtil;

/**
 * @author PENGYAO
 * 
 */
@Repository("dataBatchDao")
public class DataBatchDao extends BatchDao {

	/**
	 * 入库SQL
	 */
	private static String sql = null;
	private static String into = null;
	private static List<String> params = null;
	private static List<String> paramsSharp = null;

	public int batchInsert(List<Map<String, Object>> list) throws Exception {
		if (null == list || list.size() == 0) {
			return 0;
		}
		List<Object[]> data = new ArrayList<Object[]>();
		for (Map<String, Object> item : list) {
			Object[] item1 = new Object[params.size()];
			for (int i = 0; i < params.size(); i++) {
				item1[i] = item.get(params.get(i));
			}
			data.add(item1);
		}
		return super.batchUpdate(sql, data);
	}
	public int batchInsertAll(List<Map<String, Object>> list) throws Exception {
		if (null == list || list.size() == 0) {
			return 0;
		}
		List<String> sqlList = new ArrayList<String>();
		for (Map<String, Object> item : list) {
			Object[] item1 = new Object[paramsSharp.size()];
			String intoTemp = into;
			for (int i = 0; i < paramsSharp.size(); i++) {
				String paramSharp = paramsSharp.get(i);
				String param = params.get(i);
				item1[i] = item.get(param);
				if (item.get(param) instanceof String) {
					intoTemp = intoTemp.replace(paramSharp, (String) item.get(param));
				} else if (item.get(param) instanceof Integer) {
					intoTemp = intoTemp.replace(paramSharp, Integer.toString((Integer) item.get(param)));
				} else if (item.get(param) instanceof Double) {
					intoTemp = intoTemp.replace(paramSharp, Double.toString((Double) item.get(param)));
				} else if (item.get(param) instanceof Long) {
					intoTemp = intoTemp.replace(paramSharp, Long.toString((Long) item.get(param)));
				} else if (item.get(param) instanceof Timestamp) {
					intoTemp = intoTemp.replace(paramSharp, ((Timestamp) item.get(param)).toString().substring(0, 19));
				}
			}
			sqlList.add(intoTemp);
		}
		return super.batchUpdateAll(sqlList);
	}

	private synchronized static List<String> genParams(String sql) {
		List<String> result = new ArrayList<String>();
		String left = "{";
		String right = "}";
		for (int i = 0; i < sql.length(); i++) {
			char item = sql.charAt(i);
			if ('#' == item) {
				// 将后面的{}中字符截取
				int start = sql.indexOf(left, i) + 1;
				int end = sql.indexOf(right, i);
				result.add(new String(sql.substring(start, end)));
				i = end;
			}
		}
		return result;
	}
	private synchronized static List<String> genParamsSharp(String sql) {
		List<String> result = new ArrayList<String>();
		String left = "{";
		String right = "}";
		for (int i = 0; i < sql.length(); i++) {
			char item = sql.charAt(i);
			if ('#' == item) {
				// 将#{}字符截取包括#{}
				int start = sql.indexOf(left, i) -1;
				int end = sql.indexOf(right, i) + 1;
				result.add(new String(sql.substring(start, end)));
				i = end;
			}
		}
		return result;
	}

	private static String genSql(String sql) {
		sql = sql.replaceAll("\\#\\{[^\\}]+\\}", "?");
		return sql;
	}

	static {
		String tempSQL = ConfigUtil.getProperty("sql");
		params = genParams(tempSQL);
		sql = genSql(tempSQL);
		into = ConfigUtil.getProperty("into");
		paramsSharp = genParamsSharp(into);
	}
}
